package com.canbot.u05.activity.guide;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.socket.StringMsgBean;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zmp on 2017/5/17.
 *  导览任务执行界面
 */

public class GuideMapTaskRunningActivity extends BaseActivityNoTitle {

        @BindView(R.id.main_tv)
        TextView mainTv;

        @BindView(R.id.guiding_tv)
        TextView guidingTv;

        @BindView(R.id.guide_runTask_stop)
        Button stopBt;

        @BindView(R.id.guide_runTask_pause_continue)
        Button pauseContinueBt;

        @BindView(R.id.guide_runTask_next)
        Button nextBt;

        @BindView(R.id.guide_task_ll)
        LinearLayout guideTaskLl;

        private ToastTools toastTools;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.guide_task_running_activity);
                ButterKnife.bind(this);
                EventBus.getDefault().register(this);
                toastTools = ToastTools.newInstance(this);
                initView();
        }

        private void initView() {
                String taskName = getIntent().getStringExtra("taskName");
                mainTv.setText(taskName);
                guidingTv.setText(getString(R.string.guiding));
                upToServerTask(5);
        }

        /**
         * 消息发布者位置
         * ReceiveData
         * mHandler
         * case MsgType.MOBILE_PAD_MAP_TASK:
         *
         * @param bean
         */
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMapEvent(StringMsgBean bean) {
                if (bean.getMsgType() == MsgType.COMPERE_RAN_TASK_END) {
                        toastTools.showText(getString(R.string.task_termination));
                        finish();
                }
        }

        @Override
        protected void onDestroy() {
                EventBus.getDefault().unregister(this);
                super.onDestroy();
        }

        private Dialog loadDialog;

        private void showLoadDialog() {
                if (loadDialog == null) {
                        loadDialog = new Dialog(this, R.style.MyDialogStyle);
                        loadDialog.setContentView(R.layout.guide_dialog_load);
                        loadDialog.setCancelable(false);
                }
                StatusBarUtil.hideVirtualKey(loadDialog);
                loadDialog.show();
        }

        private void upToServerTask(final int type) {
                showLoadDialog();
                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", type);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                                loadDialog.dismiss();
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                handleResult(type);
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                loadDialog.dismiss();
                                if (type == 5 || type == 15) {
                                        finish();
                                }
                                toastTools.showText(getString(R.string.service_error_hint));
                        }
                });
        }

        private void handleResult(int type) {
                switch (type) {
                        case 5://执行任务
                                break;
                        case 15://退出
                                //                                finish();
                                break;
                        case 16://暂停或者继续
                                pauseContinueBt.setSelected(!pauseContinueBt.isSelected());
                                pauseContinueBt.setText(pauseContinueBt.isSelected()?getString(R.string.guiding):getString(R.string.guide_pause));
                                guidingTv.setText(pauseContinueBt.isSelected()?getString(R.string.guide_pause):getString(R.string.guiding));
                                break;
                        case 17://下一个
                                pauseContinueBt.setSelected(false);
                                pauseContinueBt.setText(getString(R.string.guide_pause));
                                guidingTv.setText(getString(R.string.guiding));
                                break;
                }
        }

        @OnClick({R.id.guide_runTask_stop, R.id.guide_runTask_pause_continue, R.id.guide_runTask_next})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.guide_runTask_stop:
                                upToServerTask(15);
                                break;
                        case R.id.guide_runTask_pause_continue:
                                upToServerTask(16);
                                break;
                        case R.id.guide_runTask_next:
                                upToServerTask(17);
                                break;
                }
        }
}
